package edu.po.au.server.data;

import java.util.Collection;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.persistence.EntityNotFoundException;

import edu.po.au.server.domain.AuctionHead;
import edu.po.au.server.domain.Product;

public class ProductDAL {

	
  public static Product getProductId (PersistenceManager pm, Long productId) throws EntityNotFoundException {
  	Product product = null;
    
    Query query = pm.newQuery(Product.class);
    query.setFilter(product.PRODUCT_Id + "=="+productId+"");

    Collection<Product> query_entities = null;
    
    query_entities = (Collection<Product>) query.execute();
    
    if (!query_entities.isEmpty()) {
    	product = pm.detachCopy(query_entities.iterator().next());
    }
    else {
      throw new EntityNotFoundException("No Product found with ProductId["+productId+"]");
    }
    
    return product;
    
  }
  
  public static Product startup(PersistenceManager pm){
  	Product product = new Product();
  	product.setCategoryId("1");
  	product.setCategoryText("Jewelry");
  	product.setProductId(1L);
  	product.setProductText("Pendant Necklace LP0001");
  	product.setProductDescription("New Product Large Pendant Charms Jewelry Round Blue Turquoise Natural");
  	
  	AuctionHead auction = new AuctionHead();
  	auction.setAuctionHeadId(1L);
  	auction.setCategoryId(1L);
  	auction.setCategoryName("jewelry");
  	auction.setProductId(1L);
  	auction.setProductText("TEST");
  	auction.setStartDate(20140201L);
  	auction.setUserId(1L);
  	pm.makePersistent(auction);
  	return product;
  }
}
